package com.cfh.web.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.cfh.ahead.Constant;
import com.cfh.domain.User;
import com.cfh.domain.UserExample;
import com.cfh.domain.UserExample.Criteria;
import com.cfh.service.UserService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserJson extends ActionSupport implements ModelDriven {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UserJson.class);
	
	private static final long serialVersionUID = 1L;
	private UserService userService;
	private Map root = new HashMap();
	private User user = new User();

	private String[] idsToDel = new String[200];
	private int start;

	private int limit;

	private String sort;

	private String dir;
	public UserJson() {
		super();

	}
	public String add() {
		logger.debug("add() - add() start"); //$NON-NLS-1$
		userService.insert(user);
		root.put("success", new Boolean(true));
		logger.debug("add() - add() end"); //$NON-NLS-1$
		return SUCCESS;
	}
	private String buildOrderString() {
		String toReturn = null;
		if (Tools.hasText(sort)) {
			toReturn = sort;
			if (Tools.hasText(dir)) {
				toReturn += " " + dir;
			}
		}
		return toReturn;
	}

	public String del() {
		logger.debug("del() - del() start"); //$NON-NLS-1$
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		List idList = new ArrayList(idsToDel.length);
		for (int i = 0; i < idsToDel.length; i++) {
			if (idsToDel[i] != null)
				idList.add(idsToDel[i]);
		}
		criteria.andLOGIN_NAMEIn(idList);
		int count = userService.deleteByExample(example);
		root.put("success", new Boolean(true));
		root.put("count", new Integer(count));

		logger.debug("del() - del() end"); //$NON-NLS-1$
		return SUCCESS;
	}

	/***
	 * nouse
	 */
	public String execute() {

		return SUCCESS;
	}

	public String getDir() {
		return dir;
	}

	public String[] getIdsToDel() {
		return idsToDel;
	}

	public int getLimit() {
		return limit;
	}

	public Object getModel() {

		return user;
	}

	public Map getRoot() {
		return root;
	}

	public String getSort() {
		return sort;
	}

	public int getStart() {
		return start;
	}

	public User getUser() {
		return user;
	}

	public UserService getUserService() {
		return userService;
	}

	/***
	 * 得到所有用户
	 */
	public String list() {

		UserExample example = new UserExample();
		example.setOrderByClause(buildOrderString());
		example.createCriteria().andLOGIN_NAMENotEqualTo("admin");
		List list = userService.selectByExample(example, start, limit);
		prepareData(list);
		//int count = userService.countByExample(example);
		
		int count = 0;
		try {
			count = userService.countByExample(example);
		} catch (Exception e) {
			count = Constant.MAX_COUNT_WHEN_EXCEPTION;
			logger.warn("list()", e); //$NON-NLS-1$
		}
		
		root.put("total", new Integer(count));
		root.put("items", list);
		//debug json date util
		//root.put("executeDate", new java.util.Date());
		return SUCCESS;
	}

	public String modify() {
		logger.debug("modify() - modify() start"); //$NON-NLS-1$
		int i = userService.update(user);
		if (i > 0) {
			root.put("success", new Boolean(true));
		} else {
			root.put("success", new Boolean(false));
		}
		logger.debug("modify() - modify() end"); //$NON-NLS-1$
		return SUCCESS;
	}

	private void prepareData(List list) {
		if (list.size() == 0)
			return;
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			User tempU = (User) iterator.next();
			tempU.setPASSWORD(null);
		}
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public void setIdsToDel(String[] idsToDel) {
		this.idsToDel = idsToDel;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
